var event = require('../../../../utils/event.js')
var regeneratorRuntime = require('../../../../lib/js/runtime-module.js')
import config from '../../../../utils/config2'
import util from '../../../../utils/util2'
import Serv from '../walletServ'
let TimerClass=util.TimerClass

let timerObj=null//计时器实例
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    isSendMsg: false,
    userInfo: {},
    countText: '发送验证码',
    token: '',//从获取验证码接口取到的token
    timeCount: 0,
    verify_code: '',
    password: {},
    password_new: {},
    inputDisable: false,
    phoneNo: '',
  },
  // 发送验证码
  sendMsg: async function () {
    let {user_id, linked_acctno, verify_code, password} = this.data
    if(timerObj==null){//计时器实例化
      timerObj=new TimerClass(60, (timeCount,countText) => {
        let disabled = countText!=='获取验证码'
        this.setData({
          timeCount: timeCount,
          disabled: disabled,
          countText: countText,
          isSendMsg: true,
        })
      })
    }
    // 计时器状态
    if (timerObj.disabled) {
      return false
    }
    timerObj.start()
    console.log('sendMsg', password)
    //发起请求
    let params = {
      user_id: user_id,
      linked_acctno: linked_acctno,//绑定的银行卡
    }
    // 发送请求
    let {success, msg, data} = await Serv.findPasswordApply(params)
    if (!success) {
      timerObj.stop()
      this.setData({isSendMsg:false,})
      wx.showToast({
        title: msg || '',
        icon: 'none',
      })
      return false
    }
  
    this.setData({token: data.token || ''})
  },
  // 表单输入
  bindinput: function (e) {
    console.log('bindinput', e.currentTarget.dataset, e.detail)
    let label = e.currentTarget.dataset.label
    let value = e.detail.value
    this.setData({
      [label]: value,
    })
  },
  
  // 修改交易密码  user_id  verify_code  password	  random_key
  findPasswordVerify: async function () {
    let {user_id, verify_code, password,password_new, random_key, token,isSendMsg} = this.data
    //发起请求
    let params = {
      user_id,
      verify_code,
      password: password.pwdMw || '',
      random_key: password.randomKey || '',
      token,
    }
  
    // 校验
    let errormsg = ''
    if (!isSendMsg) {
      errormsg = '请获取验证码'
    }
    else if (verify_code === '') {
      errormsg = '请输入验证码'
    }
    else if (password.pwdMw == '') {
      errormsg = '输入交易密码'
    }
    else if (password_new.pwdMw == '') {
      errormsg = '输入确认密码'
    }
    else if (password.pwdHash !== password_new.pwdHash) {
      errormsg = '两次输入密码不一致'
    }
    if (errormsg) {
      wx.showToast({
        title: errormsg || '',
        icon: 'none',
      })
      return false
    }
    
    let {success, msg, data} = await Serv.findPasswordVerify(params)
    if (!success) {
      wx.showToast({
        title: msg || '',
        icon: 'none',
      })
      return false
    }
    wx.showModal({
      title: '提示',
      content: '操作成功',
      showCancel: false,
      complete: (res)=> {
        wx.redirectTo({
          url: '../index/index',
        })
      }
    })
  },
  // 已绑定的银行卡
  queryLinkedAcct: async function (user_id) {
    // let {user_id,}=this.data
    //发起请求
    let params = {
      user_id,
    }
    let {success, msg, data} = await Serv.queryLinkedAcct(params)
    if (!success) {
      wx.showToast({
        title: msg || '',
        icon: 'none',
      })
      return false
    }
    let list = data.linked_acctlist || []
    this.setData({linked_acctno: list[0] && list[0]['linked_acctno'] || ''})
  },
  
  //唤起密码控件
  pwdFocus: function (e) {
    let label = e.currentTarget.dataset.label
    // from：当前地址， eventName: 监听定义的事件名
    util.navigateStateChange(this, 'inputDisable')
    wx.navigateTo({
      url: '../../pwdCtrl/pwdCtrl?from=/wallet/password/password&eventName=onPwdChange' + `&label=${label}`
    })
  },
  //密码控件回调
  pwdCtrlCallback: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user_id = wx.getStorageSync(config.userIdKey)
    let phoneNo = (wx.getStorageSync('walletInfo')||{}).mobile
    // console.log('onLoad user_id',user_id)
    this.setData({user_id,phoneNo})
    this.queryLinkedAcct(user_id)
  
    //监听密码控件输入
    var that = this
    event.on('onPwdChange', that, function (changedData={}) {
      let {pwdMw = '', label = '', randomKey = '',pwdHash='',pwdlen=0} = changedData
      console.log('DataChanged trigger...', changedData, {
        [label]: pwdMw
      })
      that.setData({
        [label]: {
          pwdMw: pwdMw,
          randomKey: randomKey,
          pwdHash: pwdHash,
          pwdlen: pwdlen,
          pwdMw_new: pwdMw.substr(0,pwdlen),
        },
      })
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    console.log('pages:', pages, 'currentPage:', currentPage)
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    //取消监听
    event.remove('onPwdChange', that)
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //
  // }
})
